(function() {
  'use strict';

  function Stats() {
  }

  Stats.prototype = {
    init : function (state) {
      this.state = state[0];
    },

    create: function () {
      this.state.fillColor = '#000';
      this.earned = 0;
      this.pieId = 0;
      this.chance = 0.05;
      this.coalCounter = 0;
      this.sellSounds = [this.add.audio('sell0'), this.add.audio('sell1')];

      var self = this;
      this.playSellSound = _.throttle(function () {
        _.sample(self.sellSounds).play();
      }, 100);


      this.coals = [];
      this.money = [];

      var titleText = 'End of day ' + this.state.day;

      this.titleTextObj = this.add.bitmapText(this.world.centerX, 20, 'font', titleText, 64);
      this.titleTextObj.x -= this.titleTextObj.textWidth / 2;
      this.titleTextObj.align = 'center';

      var totalCollectedText = 'You collected 0 pieces of coal';
      this.totalCollectedTextObj = this.add.bitmapText(this.world.centerX, this.world.height - 128, 'font', totalCollectedText, 48);
      this.totalCollectedTextObj.x -= this.totalCollectedTextObj.textWidth / 2;
      this.totalCollectedTextObj.align = 'center';

      
      this.updateFn = this.countUpAllCoal;

      if(this.state.day === 0) {
        this.game.state.start('office', true, false, [this.state]);
        return;
      }
    }, 

    createCoal : function createCoalFn(id){
      var spr = this.add.sprite(0, 0, 'sml', id);
      spr.width = 32;
      spr.height = 32;

      spr.x = this.world.centerX;
      spr.y = 120;
      spr.dx = Math.random() * 10 - 5;
      spr.dy = 0.0;

      spr.groundy = this.world.height - 250 + Math.random() * 50;

      this.coals.push(spr);

    },

    update : function updateFn(){
      this.updateFn();
    },

    countUpAllCoal: function () {
      if(this.pieId < this.state.coal.length) {
        if(Math.random() < this.chance) {
          this.coalCounter += 1;
          this.chance *= 1.1;
          this.createCoal(this.state.coal[this.pieId]);
          this.pieId += 1;
          this.totalCollectedTextObj.text = 'You collected ' + this.coalCounter + ' pieces of coal';
          this.totalCollectedTextObj.x = this.world.centerX - this.totalCollectedTextObj.textWidth / 2;
          this.totalCollectedTextObj.align = 'center';

          if(this.pieId === this.state.coal.length) {
            var self = this;
            setTimeout(function() {
              self.updateFn = self.sellAllCoal;
              self.chance = 0.1;
            }, 3000);
          }
        }
      }


      for(var i = 0 ; i < this.coals.length ; i ++) {
        var coal = this.coals[i];

        coal.x += coal.dx;
        coal.y += coal.dy;
        coal.dx *= 0.99;
        coal.dy *= 0.99;
        coal.dy += 0.15;
        if(coal.y > coal.groundy) {
          coal.dy *= -0.5;
          coal.dx *= 0.5;
        }
      }
    },

    createMoney : function createMoneyFn(coal){
      var value = 100 + (Math.random() - 0.5) * 20.0;
      
      if(Math.random() < 0.05) {
        value *= 2.0;
      }

      value |= 0;
      this.earned += value;

      var spr = this.add.bitmapText(coal.x, coal.y, 'font', '+ ' + value + '$', value / 5);
      spr.dy = -2.0;
      spr.time = 1.0;
      this.money.push(spr);
    },

    sellAllCoal : function sellAllCoalFn(){
      if(this.pieId >= 0) {
        if(Math.random() < this.chance) {
          this.chance *= 1.1;
          this.pieId -= 1;

          var coal = this.coals[this.pieId];
          if(coal === undefined){
            var self = this;
            this.totalCollectedTextObj.a = 1.0;
            setTimeout(function() {
              self.updateFn = null;
              self.state.earned = self.earned;
              self.game.state.start('office', true, false, [self.state]);
            }, 1500);
            return;
          }

          this.createMoney(coal);
          coal.kill();
          this.playSellSound();
        }
      }

      for (var i = this.money.length - 1; i >= 0; i--) {
        var text = this.money[i];
        if(!text.exists) {
          continue;
        }
        text.y += text.dy;
        text.time *= 0.95;
        text.alpha = text.time;

        if(text.time < 0.01) {
          text.destroy();
        }
      }
    },
    removeCounter : function overviewFn(){
      this.totalCollectedTextObj.a *= 0.95;
      this.totalCollectedTextObj.alpha = this.totalCollectedTextObj.a;
      this.titleTextObj.alpha = this.totalCollectedTextObj.alpha;
      if(this.totalCollectedTextObj.alpha < 0.01) {
        this.totalCollectedTextObj.destroy();
        this.paper = this.add.sprite(0, 10, 'paper', 2);

        this.paperText = [
          {
            spr : this.add.bitmapText(0, 0, 'font_black', 'assets', 48),
            x : 40,
            y : 40
          },
          {
            spr : this.add.bitmapText(0, 0, 'font_black', 'assets ' + this.state.money + ' $', 32),
            x : 50,
            y : 120
          },
          {
            spr : this.add.bitmapText(0, 0, 'font_black', 'income ' + this.earned + ' $', 32),
            x : 50,
            y : 170
          },
          {
            spr : this.add.bitmapText(0, 0, 'font_black', 'outstanding ', 48),
            x : 40,
            y : 210
          },
          {
            spr : this.add.bitmapText(0, 0, 'font_black', 'salaries ' + this.state.employees.length * this.state.employeeSalery + ' $', 32),
            x : 50,
            y : 290
          },
          {
            spr : this.add.bitmapText(0, 0, 'font_black', 'maintainance ' + this.state.maintainance + ' $', 32),
            x : 50,
            y : 340
          },
          {
            spr : this.add.bitmapText(0, 0, 'font_black', 'balance', 48),
            x : 40,
            y : 380
          },
          {
            spr : this.add.bitmapText(0, 0, 'font_black', 'income ' + (this.earned - this.state.employees.length * this.state.employeeSalery - this.state.maintainance) + ' $', 32),
            x : 50,
            y : 470
          }
        ];


        this.paper.width = 16 * 5 * 6.5;
        this.paper.height = 19 * 5 * 6.5;


        this.paper.x = -this.paper.width;
        this.paper.timer = 1.0;
        this.paper.texture.baseTexture.scaleMode = 1;
          
        this.updateFn = this.showPaper;
      }
    },
    showPaper : function overviewFn(){
      if(this.paper.dontUpdateOverFn === true) return;
      this.paper.timer *= 0.90;
      this.paper.x = -this.paper.width * this.paper.timer + (this.world.centerX - this.paper.width * 0.5) * (1.0 - this.paper.timer);

      for (var i = this.paperText.length - 1; i >= 0; i--) {
        var text = this.paperText[i];
        text.spr.x = this.paper.x + text.x;
        text.spr.y = this.paper.y + text.y;
      }

      if(this.paper.timer < 0.01 && this.paper.done !== true) {
        this.paper.done = true;
        var self = this;
        setTimeout(function(){
          self.clickToContinue = self.add.bitmapText(self.paper.x + 50, 550, 'font_black', 'Click to sign', 32);
          self.clickToContinue.alpha = 0.0;
          self.updateFn = self.showClickToContinue;

          self.input.onDown.add(function clickFn(){
            if(self.paper.dontUpdateOverFn === true) return;
            self.paper.dontUpdateOverFn = true;
            setTimeout(function (argument) {
              self.updateFn = self.sign;
              setTimeout(function gotoOfficeFn() {
                self.game.state.start('office', true, false, [self.state]);
              }, 1500);
            }, 500);
            self.paper.timer = 1.0;
            self.clickToContinue.alpha = 1.0;
            self.state.money += self.earned;
            self.state.money -= self.state.employees.length * self.state.employeeSalery + self.state.maintainance;
            self.clickToContinue.text = 'uuddlrlr bastart';
          });

        }, 1500);
      }
    },
    showClickToContinue : function showClickToContinueFn(){
      if(this.paper.dontUpdateOverFn === true) return;
      this.clickToContinue.alpha = Date.now() % 1000 > 500 ? 1.0 : 0.0;
    },
    sign : function signFn(){
        this.paper.timer *= 0.95;

        this.paper.x = -(this.paper.width + 20) * (1.0 - this.paper.timer) + this.paper.timer * (this.world.centerX - this.paper.width * 0.5);
        this.clickToContinue.x = this.paper.x + 50;
        
        for (var i = this.paperText.length - 1; i >= 0; i--) {
          var text = this.paperText[i];
          text.spr.x = this.paper.x + text.x;
          text.spr.y = this.paper.y + text.y;
        }

       var self = this;
    }
  };
  window.beneath = window.beneath || {};
  window.beneath.Stats = Stats;


}());
